﻿using System;
using GameEngine.Model;
using GameEngine.Model.Data;
using GameEngine.View;
using GameEngine.View.Components;
using PureMVC.Interfaces;
using PureMVC.Patterns;

namespace GameEngine.Controller
{
    public class DestroyWorldCommand<MyIdType> : SimpleCommand
        where MyIdType : IComparable<MyIdType>
    {
        override public void Execute(INotification notification)
        {
            IWorldVO<MyIdType> world = notification.Body as IWorldVO<MyIdType>;

            IWorldDataProxy<MyIdType> worldDataProxy = Facade.RetrieveProxy(IWorldDataProxyConstants<MyIdType>.CreateDataProxyName(world)) as IWorldDataProxy<MyIdType>;
            IMapDataProxy<MyIdType> mapDataProxy = Facade.RetrieveProxy(IMapDataProxyConstants<MyIdType>.CreateDataProxyName(worldDataProxy.Map.VO)) as IMapDataProxy<MyIdType>;

            foreach (ThingVO<MyIdType> thing in world.Items)
            {
                SendNotification(ApplicationFacade.DESTROYTHING_COMMAND, thing.Id);
                //IThingDataProxy<MyIdType> thingDataProxy = ModelFacade<MyIdType>.Helper.RetrieveThingDataProxy(thing);
                //worldDataProxy.AddItem(thingDataProxy);
            }

            Facade.RemoveMediator(WorldMediatorConstants<MyIdType>.GetMediatorNameFromProxy(worldDataProxy));
            Facade.RemoveMediator(MapMediatorConstants<MyIdType>.GetMediatorNameFromProxy(mapDataProxy));
            Facade.RemoveMediator(IThingMediatorConstants<MyIdType>.GetMediatorNameFromProxy(worldDataProxy));

            Facade.RemoveProxy(IMapDataProxyConstants<MyIdType>.CreateDataProxyName(worldDataProxy.Map.VO));
            Facade.RemoveProxy(IWorldDataProxyConstants<MyIdType>.CreateDataProxyName(world));
            ////SendNotification(ApplicationFacade.WORLDADDED_NOTIFICATION, world);
        }
    }
}
